define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init();
            
            //绑定事件
            $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                var panel = $($(this).attr("href"));
                if (panel.size() > 0) {
                    Controller.table[panel.attr("id")].call(this);
                    $(this).on('click', function (e) {
                        $($(this).attr("href")).find(".btn-refresh").trigger("click");
                    });
                }
                //移除绑定的事件
                $(this).unbind('shown.bs.tab');
            });
            
            //必须默认触发shown.bs.tab事件
            $('ul.nav-tabs li.active a[data-toggle="tab"]').trigger("shown.bs.tab");
        },
        table: {
            first: function () {
                // 表格1
                var table1 = $("#table1");
                table1.bootstrapTable({
                    url: 'system/blacklist/index', //调用控制器
                    extend: {   //调用html
                        index_url: 'system/blacklist/index',
                        add_url: 'system/blacklist/add',
                        edit_url: 'system/blacklist/edit',
                        del_url: 'system/blacklist/del',
                        multi_url: 'system/blacklist/multi',
                        table: 'v_blacklist',
                    },
                    toolbar: '#toolbar1',
                    pk: 'uid',
                    sortName: 'uid',
                    search: false,
                    columns: [
                        [
                            {field: 'state', checkbox: true, },
                            {field: 'uid', title: __('Uid')}, 
                            {field: 'nickname', title: __('Nickname')},
                            {field: 'createtime', title: __('Createtime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange'},                           
                            {field: 'endtime', title: __('Endtime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange'},
                            {field: 'remark', title: __('Remark'), operate: 'LIKE %...%', placeholder: '模糊搜索，*表示任意字符'}, 
                            {field: 'operate', title: __('Operate'), table: table1, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                        ]
                    ]
                });

                // 为表格1绑定事件
                Table.api.bindevent(table1);
            },
            second: function () {
                // 表格2
                var table2 = $("#table2");
                table2.bootstrapTable({
                    url: 'system/whitelist/index',
                    extend: {
                        index_url: 'system/whitelist/index',
                        add_url: 'system/whitelist/add',
                        edit_url: 'system/whitelist/edit',
                        del_url: 'system/whitelist/del',
                        multi_url: 'system/whitelist/multi',
                        table: 'v_whitelist',
                    },
                    toolbar: '#toolbar2',
                    pk: 'uid',
                    sortName: 'uid',
                    search: false,
                    columns: [
                        [   
                            {field: 'state', checkbox: true, },
                            {field: 'uid', title: __('Uid')}, 
                            {field: 'nickname', title: __('Nickname')},                           
                            {field: 'operate', title: __('Operate'), table: table2, events: Table.api.events.operate, formatter: Table.api.formatter.operate}

                        ]
                    ]
                });

                // 为表格2绑定事件
                Table.api.bindevent(table2);
            },
             firstrecord: function () {
                // 表格3
                var table3 = $("#table3");
                table3.bootstrapTable({
                    url: 'system/blacklistcord/index',
                    extend: {
                        index_url: 'system/blacklistcord/index',
                        add_url: 'system/blacklistcord/add',
                        edit_url: 'system/blacklistcord/edit',
                        del_url: 'system/blacklistcord/del',
                        multi_url: 'system/blacklistcord/multi',
                        table: 'v_blacklistcord',
                    },
                    toolbar: '#toolbar3',
                    pk: 'id',
                    sortName: 'id',
                    search: false,
                    columns: [
                        [   
                            {field: 'uid', title: __('Uid')},
                            {field: 'nickname', title: __('Nickname')},
                            {field: 'createtime', title: __('Createtime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange'},
                            {field: 'endtime', title: __('Endtime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange'},        
							{field: 'remark', title: __('Remark'), operate: 'LIKE %...%', placeholder: '模糊搜索，*表示任意字符'}, 	
                        ]
                    ]
                });
                // 为表格3绑定事件
                Table.api.bindevent(table3);
            }
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
        }
    };
    return Controller;
});